<?php
declare (strict_types = 1);

namespace app\admin\middleware;
use think\facade\Cookie;
use think\facade\Db;
use think\facade\Session;


class UrlCheck
{
    use \liliuwei\think\Jump;
    public function handle($request, \Closure $next)
    {
        $user = $request->user;
        if($user['admin_name'] != 'admin'){
        $now_url = $request->baseUrl();
        $now_url = strtolower($now_url);


        $have_url = $url = Db::table('admin')
            ->alias('a')
            ->join('admin_role a_r','a.id=a_r.admin_id')
            ->join('role_node r_n','r_n.role_id=a_r.role_id')
            ->join('node n','r_n.node_id=n.id')
            ->where('a.id',$user['id'])
            ->field('n.node_url')
            ->select()->toArray();

        $url = [];

        $ex_url = explode('/',$now_url);
        //无限极分类这些方法拼接
        $url[] = '/'.$ex_url[1].'/'.$ex_url[2].'/'.'data';
        $url[] = '/'.$ex_url[1].'/'.$ex_url[2].'/'.'record';


        foreach ($have_url as $v){
            $url[] = $v['node_url'];
        }
        $now_url = explode('.',$now_url);

        if(!in_array($now_url[0],$url)){
            $this->error('没有权限','/admin/home/index');
        }
        }

        return $next($request);
    }
}
